package microassembler;

import java.io.PrintStream;
import java.util.*;

class microinst
{

    public microinst(String s)
        throws InvalidMicroInstException
    {
        lineLabel = null;
        microInst = "";
        branchCond = null;
        branchLabel = null;
        theBranches = new branchcond();
        int i = 0;
        int j = 0;
        Vector vector = new Vector();
        rawLine = s.replace('\t', ' ').trim();
        for(StringTokenizer stringtokenizer = new StringTokenizer(rawLine, " "); stringtokenizer.hasMoreTokens(); vector.addElement(((Object) (stringtokenizer.nextToken())))) { }
        String s1 = (String)vector.elementAt(0);
        if(s1.endsWith(":"))
        {
            lineLabel = s1.substring(0, s1.length() - 1);
            i = 1;
        }
        int l = vector.size() - 1;
        for(String s2 = (String)vector.elementAt(l); l > i && validSubMicroInst(s2) != -1; s2 = (String)vector.elementAt(l))
        {
            l--;
        }

        j = l;
        int k = -1;
        String s7 = "";
        for(int i1 = i; i1 <= j; i1++)
        {
            String s3 = (String)vector.elementAt(i1);
            if(k == -1)
            {
                k = validSubMicroInst(s3);
                if(k != -1)
                {
                    s7 = s3;
                }
            }
            microInst = microInst + (String)vector.elementAt(i1);
        }

        if(k != -1)
        {
            String s4 = getErrorIndicator(s7, k);
            if(s4.equals("V"))
            {
                throw new InvalidMicroInstException("Micro-instruction contains characters other than [0,1]." + globals.ENDL + "\tPossibly missing : after label?" + globals.ENDL + "\t" + s4 + globals.ENDL + "\t" + rawLine);
            } else
            {
                throw new InvalidMicroInstException("Micro-instruction contains characters other than [0,1]." + globals.ENDL + "\t" + s4 + globals.ENDL + "\t" + rawLine);
            }
        }
        if(vector.size() > j + 1)
        {
            branchCond = (String)vector.elementAt(j + 1);
            if(((Hashtable) (theBranches)).get(((Object) (branchCond))) == null)
            {
                String s5 = getErrorIndicator(branchCond, 0);
                throw new InvalidMicroInstException("Unrecognized branch condition." + globals.ENDL + "\t" + s5 + globals.ENDL + "\t" + rawLine);
            }
        }
        if(vector.size() > j + 2)
        {
            branchLabel = (String)vector.elementAt(j + 2);
        }
        if(branchCond != null && !branchCond.equals("toOpcode") && branchLabel == null)
        {
            String s6 = getErrorIndicator(branchCond, branchCond.length() + 1);
            throw new InvalidMicroInstException("Missing branch label." + globals.ENDL + "\t" + s6 + globals.ENDL + "\t" + rawLine);
        } else
        {
            return;
        }
    }

    public String getLineLabel()
    {
        return lineLabel;
    }

    public String getMicroInst()
    {
        return microInst;
    }

    public String getBranchCondition()
    {
        return branchCond;
    }

    public String getBranchLabel()
    {
        return branchLabel;
    }

    private String getErrorIndicator(String s, int i)
    {
        String s1 = "";
        int j = rawLine.indexOf(s);
        for(int k = 0; k < j + i; k++)
        {
            s1 = s1 + " ";
        }

        s1 = s1 + "V";
        return s1;
    }

    private int validSubMicroInst(String s)
    {
        for(int i = 0; i < s.length(); i++)
        {
            if(s.charAt(i) != '1' && s.charAt(i) != '0')
            {
                return i;
            }
        }

        return -1;
    }

    public String toString()
    {
        return lineLabel + " \t" + microInst + " \t" + branchCond + " \t" + branchLabel;
    }

    public static void main(String args[])
    {
        try
        {
            microinst microinst1 = new microinst("LABEL1: 0000111100001111 onNeg BRANCH1");
            System.out.println(microinst1.toString());
            microinst microinst7 = new microinst("LABEL2: 1111000011110000 onZero BRANCH2");
            System.out.println(microinst7.toString());
            microinst microinst8 = new microinst("1010 1010 1010 1010 onReady BRANCH3");
            System.out.println(microinst8.toString());
            microinst microinst9 = new microinst("0101 0101 0101 0101 onAll BRANCH4");
            System.out.println(microinst9.toString());
            microinst microinst10 = new microinst("1010 1010 1010 1010 0010 0010 1101");
            System.out.println(microinst10.toString());
            microinst microinst11 = new microinst("1010 1010 1010 1010 toOpcode");
            System.out.println(microinst11.toString());
        }
        catch(InvalidMicroInstException invalidmicroinstexception)
        {
            System.out.println("Error: " + invalidmicroinstexception.cause);
        }
        try
        {
            microinst microinst2 = new microinst("LABEL1: 00ABC111100001111 onInt BRANCH1");
            System.out.println(microinst2.toString());
        }
        catch(InvalidMicroInstException invalidmicroinstexception1)
        {
            System.out.println("Error: " + invalidmicroinstexception1.cause);
        }
        try
        {
            microinst microinst3 = new microinst("LABEL3: 00 ABC 111100001111 onInt BRANCH3");
            System.out.println(microinst3.toString());
        }
        catch(InvalidMicroInstException invalidmicroinstexception2)
        {
            System.out.println("Error: " + invalidmicroinstexception2.cause);
        }
        try
        {
            microinst microinst4 = new microinst("LABEL1: 0000111100001111 onAll");
            System.out.println(microinst4.toString());
        }
        catch(InvalidMicroInstException invalidmicroinstexception3)
        {
            System.out.println("Error: " + invalidmicroinstexception3.cause);
        }
        try
        {
            microinst microinst5 = new microinst("LABEL1: 0000111100001111 BRANCH2");
            System.out.println(microinst5.toString());
        }
        catch(InvalidMicroInstException invalidmicroinstexception4)
        {
            System.out.println("Error: " + invalidmicroinstexception4.cause);
        }
        try
        {
            microinst microinst6 = new microinst("LABEL1 0000111100001111 onReady BRANCH2");
            System.out.println(microinst6.toString());
        }
        catch(InvalidMicroInstException invalidmicroinstexception5)
        {
            System.out.println("Error: " + invalidmicroinstexception5.cause);
        }
    }

    private String lineLabel;
    private String microInst;
    private String branchCond;
    private String branchLabel;
    private branchcond theBranches;
    private String rawLine;
}
